<?php
(!defined('IN_TOA') || !defined('IN_ADMIN')) && exit('Access Denied!');

get_config_keytable("workclass");
		if (user_union_id!='') {
			$wheresqltt = "  a.unionid='".user_union_id."'";
			$wheresqlsss = "  unionid='".user_union_id."'";
		}else{
			$wheresqltt = "  a.unionid='".union_id."'";
			$wheresqlsss = "  unionid='".union_id."'";
		}
function get_inc_user_types($fid=0)
{
    global $db;
	$html='';
	$query = $db->query("SELECT title FROM ".DB_TABLEPRE."workclass_template where id='".$fid."' ORDER BY id desc limit 0,1");
	while ($rowuser = $db->fetch_array($query)) {
	$html .= $rowuser[title];
	}
	return $html;
}
empty($do) && $do = 'list';
if ($do == 'list') {
	//列表信息 
	$wheresql = '';
	$page = max(1, getGP('page','G','int'));
	$pagesize = $_CONFIG->config_data('pagenum');
	$offset = ($page - 1) * $pagesize;
	$url = 'admin.php?ac=office&fileurl=work';
	if(!is_superadmin() && user_union_id==''){
		$wheresql .= " AND b.id =a.worktemplate and b.workmonitor like '%".GET_INC_USER_NAME($_USER->id)."%'";
	}else{
		$wheresql .= " AND b.id =a.worktemplate";
	}
	if ($title = getGP('title','G')) {
		$wheresql .= " AND a.title LIKE '%$title%'";
		$url .= '&title='.rawurlencode($title);
	}
	if ($workclasstype = getGP('workclasstype','G')) {
		$wheresql .= " AND a.workclasstype='".$workclasstype."'";
		$url .= '&workclasstype='.rawurlencode($workclasstype);
	}
	if ($worktemplate = getGP('worktemplate','G')) {
		$wheresql .= " AND a.worktemplate='".$worktemplate."'";
		$url .= '&worktemplate='.rawurlencode($worktemplate);
	}
	if ($number = getGP('number','G')) {
		$wheresql .= " AND a.number='".$number."'";
		$url .= '&number='.rawurlencode($number);
	}
	$vstartdate = getGP('vstartdate','G');
	$venddate = getGP('venddate','G');
	if ($vstartdate!='' && $venddate!='') {
		$wheresql .= " AND (a.date>='".$vstartdate."' and a.date<='".$venddate."')";
		$url .= '&vstartdate='.$vstartdate.'&venddate='.$venddate;
	}
	$ischeck = getGP('ischeck','G');
	$url .= '&ischeck='.$ischeck;
	if ($ischeck=='1') {
		$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 1 DAY)<=date(a.date) ";		
	}
	if ($ischeck=='2') {
		$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=date(a.date) ";	
	}
	if ($ischeck=='3') {
		$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 7 DAY)<=date(a.date) ";	
	}
	if ($ischeck=='4') {
		$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(a.date) ";	
	}
	if ($ischeck=='5') {
		$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 6 MONTH)<=date(a.date) ";	
	}
	//集团条件
	if (user_union_id!='') {
		$wheresql .= " AND a.unionid='".user_union_id."'";
	}else{
		$wheresql .= " AND a.unionid='".union_id."'";
	}
	$num = $db->result("SELECT COUNT(*) AS num FROM ".DB_TABLEPRE."workclass a,".DB_TABLEPRE."workclass_template b WHERE 1 $wheresql and b.tmpytpe='1'");
    $sql = "SELECT a.* FROM ".DB_TABLEPRE."workclass a,".DB_TABLEPRE."workclass_template b WHERE 1 $wheresql and b.tmpytpe='1' ORDER BY a.id desc LIMIT $offset, $pagesize";
	$result = $db->fetch_all($sql);
	if (user_union_id!='') {
			$wheresqls = "  unionid='".user_union_id."'";
		}else{
			$wheresqls = "  unionid='".union_id."'";
		}
	//按类别
	$classlist = array();
	$sql = "SELECT * FROM ".DB_TABLEPRE."workclass_type where ".$wheresqls." ORDER BY id asc";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$classlist[] = $row;
	}
	//按类型
	$typelist = array();
	$sql = "SELECT * FROM ".DB_TABLEPRE."workclass_template where ".$wheresqls." ORDER BY id asc";
	$query = $db->query($sql);
	while ($row = $db->fetch_array($query)) {
		$typelist[] = $row;
	}
	include_once('template/office.php');

} elseif ($do == 'update') {
	get_union_add();
	get_config_keytable("workclass_delete");
	$idarr = getGP('id','P','array');
	foreach ($idarr as $id) {
		$db->query("DELETE FROM ".DB_TABLEPRE."workclass WHERE id = '$id' AND unionid='".union_id."'");
		$db->query("DELETE FROM ".DB_TABLEPRE."workclass_db WHERE workclassid= '$id' AND unionid='".union_id."'");
		$db->query("DELETE FROM ".DB_TABLEPRE."workclass_personnel WHERE workclassid= '$id' AND unionid='".union_id."'");
		$content=$id;
		$title='删除工作流';
		get_oa_log_dbadd($id,$content,$title,35,$_USER->id);	
	}
    show_msg('工作流信息删除成功！', 'admin.php?ac='.$ac.'&fileurl='.$fileurl.'');

}elseif ($do == 'excel') {
	$datename="workclass_".get_date('Y',PHP_TIME).get_date('m',PHP_TIME).get_date('d',PHP_TIME).get_date('H',PHP_TIME).get_date('i',PHP_TIME).get_date('s',PHP_TIME);
	$outputFileName = 'excel/'.$datename.'.xls';
		$wheresql = '';
		if(!is_superadmin() && user_union_id==''){
			$wheresql .= " AND b.id =a.worktemplate and b.workmonitor like '%".GET_INC_USER_NAME($_USER->id)."%'";
		}
		if (user_union_id!='') {
			$wheresql .= " AND unionid='".user_union_id."'";
		}else{
			$wheresql .= " AND unionid='".union_id."'";
		}
		if ($title = getGP('title','P')) {
			$wheresql .= " AND a.title LIKE '%$title%'";
		}
		if ($workclasstype = getGP('workclasstype','P')) {
			$wheresql .= " AND a.workclasstype='".$workclasstype."'";
		}
		if ($number = getGP('number','P')) {
			$wheresql .= " AND a.number='".$number."'";
		}
		$vstartdate = getGP('vstartdate','P');
		$venddate = getGP('venddate','P');
		if ($vstartdate!='' && $venddate!='') {
			$wheresql .= " AND (a.date>='".$vstartdate."' and a.date<='".$venddate."')";
		}
		$ischeck = getGP('ischeck','P');
		if ($ischeck=='1') {
			$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 1 DAY)<=date(a.date) ";		
		}
		if ($ischeck=='2') {
			$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 3 DAY)<=date(a.date) ";	
		}
		if ($ischeck=='3') {
			$wheresql .= " AND DATE_SUB(CURDATE(), INTERVAL 7 DAY)<=date(a.date) ";	
		}
		if ($ischeck=='4') {
			$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 1 MONTH)<=date(a.date) ";	
		}
		if ($ischeck=='5') {
			$wheresql .= " AND DATE_SUB(CURDATE(),INTERVAL 6 MONTH)<=date(a.date) ";	
		}
		if (getGP('worktemplate','P')!='') {
			//获取表单
			$archive = array();
			$inputname = array();
			$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_form where template_id='".getGP('worktemplate','P')."' AND ".$wheresqlsss." ORDER BY id Asc");
			$archive[]="<b>流水单号</b>";
			$archive[]="<b>工作流名称</b>";
			$num=0;
			while ($row = $db->fetch_array($query)) {
				$num++;
				$archive[]="<b>".$row['formname']."</b>";
				$inputname[]=$row['inputname'];
			}
			$archive[]="<b>申请时间</b>";
			$archive[]="<b>申请人</b>";
			$content = array();
			$content[] = $archive;
			$sql = "SELECT a.* FROM ".DB_TABLEPRE."workclass a,".DB_TABLEPRE."workclass_template b WHERE 1 $wheresql AND worktemplate='".getGP('worktemplate','P')."'  ORDER BY a.id desc";
			$result = $db->query($sql);
			while ($row = $db->fetch_array($result)) {	
				$archive = array();
				$archive[]=$row['number'];
				$archive[]=$row['title'];
				for($i=0;$i<$num;$i++){
					$blog = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_db  WHERE workclassid = '".$row['id']."' and inputname='".$inputname[$i]."' AND ".$wheresqlsss."");
					if($blog['type']=='3'){
						$archive[]=str_replace("-",".",$blog['inputvalue']);
					}else{
						$archive[]=$blog['inputvalue'];
					}
				}
				$archive[]=str_replace("-",".",$row['date']);
				$archive[]=GET_INC_USER_NAME($row['uid']);
				$content[] = $archive;
			}
		}else{
			//处理多模板业务
			//获取模板
			if(getGP('workclasstype','P')!=''){
				$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_template where workclasstype='".getGP('workclasstype','P')."' AND ".$wheresqlsss." ORDER BY id Asc");
			}else{
				$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_template AND ".$wheresqlsss." ORDER BY id Asc");
			}
			$templateid=array();
			$templatename=array();
			$templatenum=0;
			while ($row = $db->fetch_array($query)) {
				$templatenum++;
				$templateid[]=$row['id'];
				$templatename[]=$row['title'];
			}
			$content = array();
			for($m=0;$m<$templatenum;$m++){
				$template = $db->fetch_one_array("SELECT a.id FROM ".DB_TABLEPRE."workclass a,".DB_TABLEPRE."workclass_template b WHERE 1 $wheresql AND worktemplate='".$templateid[$m]."'  ORDER BY a.id desc");
				if($template['id']!=''){
					$content[] =array("<font size=3 color=red><b>".$templatename[$m]."</b></font>");
					$archive = array();
					$inputname = array();
					$query = $db->query("SELECT * FROM ".DB_TABLEPRE."workclass_form where template_id='".$templateid[$m]."' AND ".$wheresqlsss." ORDER BY id Asc");
					$archive[]="<b>流水单号</b>";
					$archive[]="<b>工作流名称</b>";
					$num=0;
					while ($row = $db->fetch_array($query)) {
						$num++;
						$archive[]="<b>".$row['formname']."</b>";
						$inputname[]=$row['inputname'];

					}
					$archive[]="<b>申请时间</b>";
					$archive[]="<b>申请人</b>";
					$content[] = $archive;
					$sql = "SELECT a.* FROM ".DB_TABLEPRE."workclass a,".DB_TABLEPRE."workclass_template b WHERE 1 $wheresql AND worktemplate='".$templateid[$m]."'  ORDER BY a.id desc";
					$result = $db->query($sql);
					while ($row = $db->fetch_array($result)) {	
						$archive = array();
						$archive[]=$row['number'];
						$archive[]=$row['title'];
						for($i=0;$i<$num;$i++){
							$blog = $db->fetch_one_array("SELECT * FROM ".DB_TABLEPRE."workclass_db  WHERE workclassid = '".$row['id']."' and inputname='".$inputname[$i]."' AND ".$wheresqlsss."");
							if($blog['type']=='3'){
								$archive[]=str_replace("-",".",$blog['inputvalue']);
							}else{
								$archive[]=$blog['inputvalue'];
							}
						}
						$archive[]=str_replace("-",".",$row['date']);
						$archive[]=GET_INC_USER_NAME($row['uid']);
						$content[] = $archive;
					}
				}
			}
		}
	$excel = new ExcelWriter($outputFileName);
	if($excel==false) 
		echo $excel->error; 
	foreach($content as $v){
		$excel->writeLine($v);
	}
	$excel->sendfile($outputFileName);
}
?>